//
// Created by lcy on 2024/8/16.
//
#include <jni.h>
#include <string>
#include "AAudioRecord.h"

using namespace aaudiodemo;

extern "C"
JNIEXPORT jlong  JNICALL
Java_com_example_aaudiodemo_MainActivity_nativeCreateAAudioRecord(JNIEnv *env, jobject thiz,
                                                                  jstring file_path,
                                                                  jint sampleRate,
                                                                  jint audioChannel,
                                                                  jint audioFormat) {
    jboolean isCopy;
    const char *convertedValue = (env)->GetStringUTFChars(file_path, &isCopy);
    std::string string = convertedValue;
    auto RecordEngine = new AAudioRecord(string,
                                         sampleRate, audioChannel, audioFormat);
    env->ReleaseStringUTFChars(file_path, convertedValue);
    bool ret = RecordEngine->Init();
    LOGI("nativeCreateAAudioRecord, init ret:%d", ret);
    if (ret) {
        return reinterpret_cast<jlong> (RecordEngine);
    } else {
        return 0;
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_aaudiodemo_MainActivity_nativeDestroyAAudioRecord(JNIEnv *env, jobject thiz,
        jlong engine_handle) {
    LOGI("nativeDestroyAAudioRecord");
    auto recordEngine = reinterpret_cast<AAudioRecord *>(engine_handle);
    delete recordEngine;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_aaudiodemo_MainActivity_nativeAAudioRecordPlay(JNIEnv *env, jobject thiz,
        jlong engine_handle) {
    LOGI("nativeAAudioRecordPlay");
    auto recordEngine = reinterpret_cast<AAudioRecord *>(engine_handle);
    recordEngine->Start();
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_aaudiodemo_MainActivity_nativeAAudioRecordPause(JNIEnv *env, jobject thiz,
        jlong engine_handle) {
    LOGI("nativeAAudioRecordPause");
    auto recordEngine = reinterpret_cast<AAudioRecord *>(engine_handle);
    recordEngine->Pause();
}

extern "C"
JNIEXPORT void JNICALL
Java_com_example_aaudiodemo_MainActivity_nativeAAudioRecordStop(JNIEnv *env, jobject thiz,
                                                                jlong engine_handle) {
    // TODO: implement nativeAAudioRecordStop()
    LOGI("nativeAAudioRecordStop");
    auto recordEngine = reinterpret_cast<AAudioRecord *>(engine_handle);
    recordEngine->Stop();
}